//
//  THFlowEarnCurrentTaskDetailVC.m
//  YJB
//
//  Created by 中电大维 on 2017/2/9.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "THFlowEarnCurrentTaskDetailVC.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"

@interface THFlowEarnCurrentTaskDetailVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
@property(nonatomic,strong)UIWebView  *webView;
/** 进度条代理者 */
@property(nonatomic,strong)NJKWebViewProgress *progressProxy;
/** 进度条 */
@property(nonatomic,strong)NJKWebViewProgressView *progressView;
@end

@implementation THFlowEarnCurrentTaskDetailVC

/*
    获取{"action":"get_traff_autoinfo","makegold":{"username":"用户名","user_info":"用户信息","phone_info":"手机信息","md5_key":"密钥"}}
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.title = @"任务详情";
    
    if (self.url) {
        if (![self.url hasPrefix:@"http"])
            self.url = [@"http://" stringByAppendingString:self.url];
        [self loadWebViewData:self.url];
    }else {
        [self loadData];
    }
}



- (void)loadWebViewData:(NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:req];
}

- (void)loadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"action"] = @"get_traff_autoinfo";
    params[@"makegold"] = @{@"username":[YJUserDefaultTool readUserName],
                            @"user_info":[YJUserDefaultTool readUserName],
                            @"phone_info":@"iOS",
                            @"md5_key":[YJUserDefaultTool readKeyID]};
    [MBProgressHUD showMessage:@"" toView:self.view];
    [YJNetworkTool postWithUrl:THURL(@"traffic.php") parameters:params success:^(id responseObject) {
        MyLog(@" ==== %@",responseObject);
        NSString *code = responseObject[@"return"][@"code"];
        [MBProgressHUD hideHUDForView:self.view];
        if (code.integerValue == 200)
        {
            NSString *urlString = responseObject[@"return"][@"data"];
            if (![urlString hasPrefix:@"http"])
                urlString = [@"http://" stringByAppendingString:urlString];
            [self loadWebViewData:urlString];
            
        }else
        {
            NSString *message = responseObject[@"return"][@"message"];
            [MBProgressHUD showError:message toView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
    }];
}

- (UIWebView *)webView
{
    if (_webView == nil)
    {
        _webView = [[UIWebView alloc]init];
        _progressProxy = [[NJKWebViewProgress alloc] init];
        _webView.delegate = _progressProxy;
        _progressProxy.webViewProxyDelegate = self;
        _progressProxy.progressDelegate = self;
        [self.view addSubview:_webView];
        _webView.backgroundColor = WHITECOLOR;
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
        }];
    }
    return _webView;
}

- (NJKWebViewProgressView *)progressView
{
    if (_progressView == nil)
    {
        _progressView = [[NJKWebViewProgressView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_W, 3)];
        _progressView.progressBarView.backgroundColor = APPCOLOR;
        [self.view addSubview:_progressView];
        [self.view bringSubviewToFront:_progressView];
        
    }
    return _progressView;
}


#pragma ------------------------ 进度条代理
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [self.progressView setProgress:progress animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
